

package com.maoshi.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.common.util.Json;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.distribution.comment.model.DistributionLevelSet;
import com.maoshi.shop.distribution.comment.service.DistributionLevelService;
import com.maoshi.shop.distribution.comment.service.DistributionLevelSetService;
import com.maoshi.shop.security.platform.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 *
 * @author lgh on 2019/04/04.
 */
@RestController
@RequestMapping("/distribution/distributionLevelSet")
@AllArgsConstructor
public class DistributionLevelSetController {

    private DistributionLevelSetService distributionLevelSetService;

    private DistributionLevelService distributionLevelService;

	/**
	 * 分页获取
	 */
    @GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('distribution:distributionLevelSet:page')")
	public ResponseEntity<IPage<DistributionLevelSet>> page(DistributionLevelSet distributionLevelSet, PageParam<DistributionLevelSet> page){
		IPage<DistributionLevelSet> list = distributionLevelSetService.page(page,new LambdaQueryWrapper<DistributionLevelSet>());
		return ResponseEntity.ok(list);
	}

	/**
	 * 获取信息(根据店铺)
	 */
	@GetMapping("/info")
	@PreAuthorize("@pms.hasPermission('distribution:distributionLevelSet:info')")
	public ResponseEntity<DistributionLevelSet> info(){
		DistributionLevelSet distributionLevelSet = distributionLevelSetService.getByShopId();
		return ResponseEntity.ok(distributionLevelSet);
	}

	/**
	 * 保存
	 */
	@PostMapping
	@PreAuthorize("@pms.hasPermission('distribution:distributionLevelSet:save')")
	public ResponseEntity<Void> save(@RequestBody @Valid DistributionLevelSet distributionLevelSet){
		distributionLevelSetService.saveByDistributionLevelSet(distributionLevelSet);
		return ResponseEntity.ok().build();
	}

	/**
	 * 修改
	 */
	@PutMapping
	@PreAuthorize("@pms.hasPermission('distribution:distributionLevelSet:update')")
	public ResponseEntity<Void> update(@RequestBody @Valid DistributionLevelSet distributionLevelSet){
		distributionLevelSet.setLevelSetConditionsSwitchJson(Json.toJsonString(distributionLevelSet.getLevelSetConditionsSwitch()));
		distributionLevelSet.setUpdateTime(new Date());
		distributionLevelSet.setShopId(Constant.PLATFORM_SHOP_ID);
		distributionLevelSet.setModifier(SecurityUtils.getSysUser().getUserId());
		distributionLevelSetService.updateByDistributionLevelSet(distributionLevelSet);
		return ResponseEntity.ok().build();
	}

	/**
	 * 删除
	 */
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('distribution:distributionLevelSet:delete')")
	public ResponseEntity<Void> delete(@PathVariable Long id){
		distributionLevelSetService.removeById(id);
		return ResponseEntity.ok().build();
	}
}
